package com.hsy.crm.web.service.asyn.marketprofit;

import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.SaleCostDetail;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.mapper.SaleCostDetailMapper;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售退货反审核
 */
@Service
public class UnSaleBackProfitService {

    @Autowired
    ProfitCommonService profitCommonService;

    @Autowired
    SaleProfitService saleProfitService;

    @Autowired
    SaleCostDetailMapper saleCostDetailMapper;

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    public void init(Message message, List<StoreProductDetail> storeProductDetails){
        for (StoreProductDetail storeProductDetail : storeProductDetails){
            init(message,storeProductDetail);
        }
    }

    private void init(Message message,StoreProductDetail storeProductDetail){
        Long saleOrderDetailId = profitCommonService.getOrderDetailId(message,storeProductDetail);
        if( saleOrderDetailId > 0 ){
            List<SaleCostDetail> saleCostDetails = profitCommonService.getOutSaleCostDetail(message,storeProductDetail);
            for (SaleCostDetail saleCostDetail : saleCostDetails){

                StoreProductDetail updateOut = new StoreProductDetail();
                updateOut.setRelationStartSort(-1L);
                updateOut.setRelationEndSort(-1L);
                updateOut.setOutNumber(-saleCostDetail.getOutNumber());
                updateOut.setOutPrice(-saleCostDetail.getOutPrice());
                updateOut.setId(saleCostDetail.getOutStoreProductDetailId());
                updateOut.setCustomerId(message.getCustomerId());
                storeProductDetailMapper.addOut(updateOut);

                SaleCostDetail targetDetail = new SaleCostDetail();
                targetDetail.setId(saleCostDetail.getSaleCostDetailId());
                targetDetail.setCustomerId(message.getCustomerId());
                targetDetail.setBackPrice(-saleCostDetail.getOutPrice());
                targetDetail.setBackNumber(-saleCostDetail.getOutNumber());
                saleCostDetailMapper.updateOut(targetDetail);

                saleCostDetailMapper.deleteByPrimaryKey(saleCostDetail);

            }

        }

        //after time re calc
        storeProductDetail.setCustomerId(message.getCustomerId());
        List<StoreProductDetail> storeProductDetails = storeProductDetailMapper.selectOutByTime(storeProductDetail);
        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            profitCommonService.clearOutStoreProductDetail(message,outStoreProductDetail,Boolean.FALSE);
        }

        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            saleProfitService.calcStoreProductDetailDone(message,outStoreProductDetail);
        }

        storeProductDetailMapper.deleteByPrimaryKey(storeProductDetail);
    }


}
